import {
    suggestion
} from '../../api/app.js'
import user from '../../utils/user'
const app = getApp()

Page({
    data: {
        // 标题
        title: '',
        // 描述
        content: '',
        // 是否需要评价功能
        opinion: false,
        suggestion: '',
        // 选择的标签等级
        selectLevel: 2,
        // 选择的标签列表
        selectLabel: [],
        // 选择的标签内容
        suggestionOption: [{
            text: '不满意',
            icon: '/assets/img/success/unsatisfactory.png',
            icon2: '/assets/img/success/unsatisfactory-s.png',
            labels: [
                '卡顿', '闪退', '功能差', '操作繁琐', '界面显示异常', '无法打开', '加载慢', '其他'
            ]
        },
        {
            text: '一般',
            icon: '/assets/img/success/average.png',
            icon2: '/assets/img/success/average-s.png',
            labels: [
                '无感', '闪退', '功能差', '操作复杂难懂', '无法打开', '效率低', '继续努力', '其他'
            ]
        },
        {
            text: '满意',
            icon: '/assets/img/success/satisfy.png',
            icon2: '/assets/img/success/satisfy-s.png',
            labels: [
                '靠谱', '便捷', '超预期', '有效解决问题', '操作流畅', '很专业', '简单易懂', '其他'
            ]
        },
        ],
        id: '',
        // 显示的图标类型，成功、失败、警告
        type: '',
    },
    onReady() {},
    onLoad(option) {
        console.log(option)
        this.setData({
            id: option.id,
            title: option.title,
            content: option.content,
            opinion: !!option.opinion,
            back: option.back,
            type: option.type,
        })
    },
    backHome() {
        if (this.data.back === 'home')
            user.reLogin().then(() => {
                setTimeout(() => {
                    wx.reLaunch({
                        url: '/pages/index/index'
                    })
                }, 1000)
            })
        else
            wx.navigateBack({
                delta: Number(this.data.back)
            })
    },
    // 提交评价
    submit() {
        let suggestionCont = ''
        for (const index of this.data.selectLabel) {
            const text = this.data.suggestionOption[this.data.selectLevel].labels[index]
            suggestionCont += text + ';'
        }
        suggestionCont += this.data.suggestion
        suggestion({
            type: 0,
            userId: user.userId,
            satisfaction: this.data.selectLevel + 1,
            suggestion: suggestionCont,
            companyId: user.companyId,
            createUserId: user.userId
        }).then(() => {
            app.toast('评价成功，正在为您跳转!')
            setTimeout(() => {
                this.backHome()
            }, 1000)
        })
    },
    // 修改评价内容
    onChangeSuggestion(event) {
        this.setData({
            suggestion: event.detail.value
        })
    },
    // 修改评价等级
    onSelectLevel(event) {
        this.setData({
            selectLevel: event.currentTarget.dataset.index,
            selectLabel: []
        })
    },
    // 选择评价标签
    onSelectLabel(event) {
        console.log(event.currentTarget.dataset.index)
        const data = event.currentTarget.dataset.index
        const index = this.data.selectLabel.indexOf(data)
        if (index === -1)
            this.data.selectLabel.push(event.currentTarget.dataset.index)
        else
            this.data.selectLabel.splice(index, 1)
        this.setData({
            selectLabel: this.data.selectLabel
        })
    },
    // 前往查看审批单
    goExamine() {
        wx.navigateTo({
            url: '/packagePay/pages/examine/detail/detail?id=' + this.data.id
        })
    }
})